/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hsc.lock.annotation;

import com.hsc.lock.interceptor.key.KeyGenerator;
import com.hsc.lock.interceptor.refuse.RefuseHandler;
import com.hsc.lock.interceptor.store.StoreErrorHandler;
import com.hsc.lock.store.LockStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.function.Supplier;

/**
 * Abstract base {@code @Configuration} class providing common structure
 * for enabling Spring's annotation-driven cache management capability.
 *
 * @author Chris Beams
 * @author Stephane Nicoll
 * @author Juergen Hoeller
 * @see com.hsc.lock.annotation.EnableCaching
 * @since 3.1
 */
@Configuration
public abstract class AbstractCachingConfiguration implements ImportAware {

    @Nullable
    protected AnnotationAttributes enableReplayLock;

    @Nullable
    protected Supplier<LockStore> lockStore;

    @Nullable
    protected Supplier<RefuseHandler> refuseEnterHandler;

    @Nullable
    protected Supplier<KeyGenerator> keyGenerator;

    @Nullable
    protected Supplier<StoreErrorHandler> errorHandler;


    @Override
    public void setImportMetadata(AnnotationMetadata importMetadata) {
        this.enableReplayLock = AnnotationAttributes.fromMap(
                importMetadata.getAnnotationAttributes(EnableReplayLock.class.getName(), false));
        if (this.enableReplayLock == null) {
            throw new IllegalArgumentException(
                    "@EnableReplayLock is not present on importing class " + importMetadata.getClassName());
        }
    }

    @Autowired(required = false)
    void setConfigurers(Collection<CachingConfigurer> configurers) {
        if (CollectionUtils.isEmpty(configurers)) {
            return;
        }
        if (configurers.size() > 1) {
            throw new IllegalStateException(configurers.size() + " implementations of " +
                    "CachingConfigurer were found when only 1 was expected. " +
                    "Refactor the configuration such that CachingConfigurer is " +
                    "implemented only once or not at all.");
        }
        com.hsc.lock.annotation.CachingConfigurer configurer = configurers.iterator().next();
        useCachingConfigurer(configurer);
    }

    /**
     * Extract the configuration from the nominated {@link com.hsc.lock.annotation.CachingConfigurer}.
     */
    protected void useCachingConfigurer(CachingConfigurer config) {
        this.lockStore = config::lockStore;
        this.refuseEnterHandler = config::refuseEnterHandler;
        this.keyGenerator = config::keyGenerator;
        this.errorHandler = config::errorHandler;
    }

}
